﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using PureMVC.Other;
using PureMVC.Interfaces;
using System;
using PureMVC.Core;

namespace PureMVC.Patterns.Facade
{
    public partial class Facade
    {
        protected static Dictionary<string, WeakLazy<IFacade>> instanceMap =
            new Dictionary<string, WeakLazy<IFacade>>();
        protected const string MULTITON_MSG = "Facade instance for this Multiton key already constructed!";

        public static bool HasCore(string _key)
        {
            return instanceMap.ContainsKey(_key) && instanceMap[_key] != null;
        }

        public static void RemoveCore(string _key)
        {
            if (!instanceMap.ContainsKey(_key)) return;
            Model.RemoveModel(_key);
            View.RemoveView(_key);
            Controller.RemoveController(_key);
            instanceMap.Remove(_key);
        }

        public static IFacade GetInstance(string _key, Func<IFacade> _facadeFactory)
        {
            if (!instanceMap.ContainsKey(_key))
            {
                _facadeFactory();
            }
            return instanceMap[_key].Value;
        }
    }
}
